package ar.edu.utn.frsf.tpae.a2012.g04.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.edu.utn.frsf.tpae.a2012.g04.dao.ClienteDao;
import ar.edu.utn.frsf.tpae.a2012.g04.dominio.Cliente;
import ar.edu.utn.frsf.tpae.a2012.g04.dominio.Cuenta;

@Component
public class ClienteHibernateDao implements ClienteDao{

	  private SessionFactory sessionFactory;
	    
	  @Autowired(required = true)
	  public void setSessionFactory(SessionFactory factory) {
	    this.sessionFactory = factory;
	  }

	  private Session getCurrentSession() {
	    return sessionFactory.getCurrentSession();
	  }
	@Override
	public Cliente persistir(Cliente cliente) {
	 if(cliente.getId()==null){
		 getCurrentSession().save(cliente);
	 }else{
		 getCurrentSession().update(cliente);
	 }
	 return cliente;
	}

	@Override
	public void eliminar(Cliente cliente) {
		getCurrentSession().delete(cliente);
	}

	@SuppressWarnings("unchecked")
	  @Override
	  public List<Cliente> listar() {
		  Query query = getCurrentSession().createQuery(
			        "from " + Cliente.class.getName());
		  return query.list();
	  }

	@Override
	public Cliente buscarPorId(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cliente buscarPorNumero(String numero) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Cuenta> listarCuentas() {
		// TODO Auto-generated method stub
		return null;
	}

}
